import http from '../utils/request'
import { Role } from './role'
import { Team } from './team'

interface LoginSuccess {
    token: string;
    lv: string;
    alias: string;
}

// 用户登录
export const userLogin = (data: any): Promise<LoginSuccess> => {
  return http.post('login', data)
}

interface RouterMeta {
    showLink: boolean;
    icon: string;
    title: string;
    rank: number
}

export interface RouterNode {
    id: string
    path: string;
    name: string;
    redirect: string;
    meta: RouterMeta;
    children: RouterNode[];
}

export interface RouterResult {
    code: number;
    info: RouterNode[];
}

// 获取路由
export const getRouters = (): Promise<RouterResult> => {
  return http.post('user/getRoutes')
}

// =============== 用户 =================
export interface User {
    id: string;
    username: string;
    nickname: string;
    status: number;
    isOnline: number;
    email: string;
    creator: string;
    creatorId: number;
    createTime: number;
}

export interface UserForm {
    id: string;
    username: string;
    nickname: string;
    password: string;
    status: number;
    email: string;
    roleIds: string[];
    teamId: string

}

export interface UserDetail {
    userInfo: User;
    roles: Role[];
    team: Team
}

interface UserPage {
    list: User[];
    total: number;
}

// 获取用户分页信息
export const getPage = (data: object): Promise<UserPage> => {
  return http.get('/user/page', { params: data })
}

// 添加用户
export const addUser = (data: UserForm) => {
  return http.post('/user/add', data)
}

// 更新用户
export const updateUser = (data: UserForm) => {
  return http.post('/user/update', data)
}

// 删除用户
export const deleteUser = (userId: string) => {
  return http.delete('/user/delete', { params: { userId } })
}

// 获取自己的账户详细信息
export const getDetail = (userId: string): Promise<UserDetail> => {
  return http.get('/user/detail', { params: { userId } })
}
